package module.ahleteView;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @Auther leiHua
 * @Date 2022/5/12 19:46
 * 运动员界面顶部的轮播图类
 *  传入一个图片路径字符串数组，new AthletImageJPanel(str[])创建出一个放有图片的JPanel
 */
public class AthleteImageJpanel extends JPanel {
    private int index=0;
    JLabel jLabel = new JLabel();

    /**
     *
     * @param imagPath 图片路径
     * @param imgWidth  图片宽度
     * @param imgHeight 图片高度
     */
    public AthleteImageJpanel (String [] imagPath,int imgWidth,int imgHeight){
        //添加轮播图的图片
        int length = imagPath.length;
        ImageIcon[] images = new ImageIcon[length];
        int i=0;
        //遍历路径创建图片图标
        for (String s : imagPath) {
            images[i]=new ImageIcon(imagPath[i]);
            i++;
        }
        for (ImageIcon image : images) {
            //缩放图片的大小
            image.setImage(image.getImage().getScaledInstance(imgWidth,imgHeight, Image.SCALE_DEFAULT));
        }

        // 计时器:定时改变图片
        Timer timer = new Timer(2000,new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                index=index%images.length;  //循环图片的下标
                jLabel.setIcon(images[index]);
                index++;
            }
        });
        timer.start();
        this.add(jLabel);
    }



    public static void main(String[] args) {
        //测试效果
        String []imageStr={"src/imag/1.jpg","src/imag/15.jpg"};
        AthleteImageJpanel athleteImageJpanel = new AthleteImageJpanel(imageStr,800,800);
        JFrame jf = new JFrame();
        jf.add(athleteImageJpanel);
        jf.setVisible(true);
        jf.setSize(1000,800);
    }
}
